package com.dhcc.crftool.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    private static final Logger LOGGER  = LoggerFactory.getLogger(Swagger2Config.class);

    @Value("${swagger.enable:true}")
    private boolean swagger_enable;

    /**
     * 通过 createRestApi函数来构建一个DocketBean
     * @return
     */
    @Bean
    public Docket createRestApi() {
        if(!swagger_enable){
            LOGGER.info("swagger enable is false.");
        }
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swagger_enable)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dhcc.crftool.controller"))
                .paths(PathSelectors.any())
                .build();
        // 添加token令牌
//                .globalOperationParameters(parameters)
//                .securityContexts(Lists.newArrayList(securityContext())).securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey()));
    }

    @Bean
    public ApiInfo apiInfo() {
        Contact contact = new Contact("dhcc","","xxx@dhcc.com.cn");
        return new ApiInfoBuilder()
                //文档首页标题
                .title("表单管理测试")
                //文档描述信息
                .description("表单管理测试")
                //创建者信息
                .contact(contact)
                //文档版本
                .version("0.0.1")
                .build();
    }
}
